package com.mdc.mansion.core.modules.restaurant.impl;

import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.minlog.Log;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.util.MD5Utils;
import com.mdc.mansion.core.common.util.RestaurantSupport;
import com.mdc.mansion.core.modules.basedata.PushMessageService;
import com.mdc.mansion.core.modules.basedata.RestaurantStaffReceiveDishesService;
import com.mdc.mansion.core.modules.restaurant.RestaurantStaffService;
import com.mdc.mansion.core.modules.restaurant.RestaurantTableService;
import com.mdc.mansion.core.modules.restaurant.dto.RestaurantStaffDTO;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.common.service.CrudService;
import com.mdc.mansion.dataaccess.modules.restaurant.dao.RestaurantStaffDao;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantStaff;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantTable;
import com.mdc.mansion.dataaccess.modules.system.entity.User;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 餐厅员工信息ServiceImpl
 * @author mingdongc
 * @version 2018-01-01
 */
@Service
public class RestaurantStaffServiceImpl extends CrudService<RestaurantStaffDao, RestaurantStaff> implements RestaurantStaffService {
    @Resource
    private RestaurantStaffDao restaurantStaffDao;
    @Resource
    private PushMessageService pushMessageService;
    @Resource
    private RestaurantTableService restaurantTableService;   
    @Autowired
    private RestaurantStaffReceiveDishesService restaurantStaffReceiveDishesService;
    
    @Override
    public RestaurantStaff getByLoginAccount(String loginAccount) {
        return restaurantStaffDao.getByLoginAccount(loginAccount);
    }

    @Override
    public RestaurantStaff findByLoginAccount(String loginAccount) {
        RestaurantStaff restaurantStaff=new RestaurantStaff();
        restaurantStaff.setLoginAccount(loginAccount);
        List<RestaurantStaff> list = this.dao.findList(restaurantStaff);
        if(CollectionUtils.isNotEmpty(list)){return list.get(0);}
        return null;
    }

    @Override
    @Transactional
    public void updateStaffRestaurantId(String restaurantId, String staffId) {
        RestaurantStaff restaurantStaff = this.get(staffId);
        restaurantStaff.setRestaurantId(restaurantId);
        this.updateSelective(restaurantStaff);
    }

    @Override
    public List<RestaurantStaffDTO> currentUserRestaurantStaffs() {
        RestaurantStaff restaurantStaff=new RestaurantStaff();
        restaurantStaff.setRestaurantId(ContextHolder.currentRestaurant().getId());
        List<RestaurantStaff> list = this.findList(restaurantStaff);
        List<RestaurantStaffDTO> restaurantStaffDTOs= Lists.newArrayList();
        if(CollectionUtils.isEmpty(list)){return restaurantStaffDTOs;}
        for(RestaurantStaff staff:list){
            if(staff.getId().equals(ContextHolder.currentRestaurantStaff().getId())){continue;}
            RestaurantStaffDTO restaurantStaffDTO=new RestaurantStaffDTO();
            BeanUtils.copyProperties(staff,restaurantStaffDTO);
            restaurantStaffDTOs.add(restaurantStaffDTO);
        }
        return restaurantStaffDTOs;
    }

    @Override
    @Transactional
    public void saveCurrentUserRestaurantStaff(RestaurantStaffDTO restaurantStaffDTO) {
        restaurantStaffDTO.verfiy();
        User currentOperationUser = RestaurantSupport.getCurrentOperationUser();
        RestaurantStaff restaurantStaff=new RestaurantStaff();
        BeanUtils.copyProperties(restaurantStaffDTO,restaurantStaff);
        restaurantStaff.setUpdateBy(currentOperationUser);
        String name=restaurantStaffDTO.getName();
        restaurantStaff.setChineseName(name);
        restaurantStaff.setEnglishName(name);
        restaurantStaff.setName(name);
        String password = restaurantStaff.getPassword();
        if(StringUtils.isNotBlank(restaurantStaffDTO.getId())){
            if(StringUtils.isNotBlank(password)){
                restaurantStaff.setPassword(MD5Utils.string2MD5(password));
            }
            this.updateSelective(restaurantStaff);
        }else{
            RestaurantStaff account = this.findByLoginAccount(restaurantStaffDTO.getLoginAccount());
            if(account!=null){throw new BusinessException(restaurantStaffDTO.getLoginAccount()+"已经存在");}
            restaurantStaff.setRestaurantId(ContextHolder.currentRestaurant().getId());
            restaurantStaff.setCreateBy(currentOperationUser);
            restaurantStaff.setPassword(MD5Utils.string2MD5(password));
            this.save(restaurantStaff);
        }
        try{
	        if(RestaurantStaff.ROLE_TYPE.CF.getVal().equals(restaurantStaffDTO.getRoleType()))
	        {
	        	RestaurantStaff account = this.findByLoginAccount(restaurantStaffDTO.getLoginAccount());
	            Map<String,String> paraMap = Maps.newHashMap();
	            paraMap.put("restaurantId", ContextHolder.currentRestaurant().getId());
	            paraMap.put("restaurantStaffId", account.getId());
	            paraMap.put("userId",currentOperationUser.getLoginName());
	            restaurantStaffReceiveDishesService.initializeStaffReceiveDishes(paraMap);
	        }
        }catch(Exception e)
		{
			Log.error("初始化厨房接收菜品失败:"+e.getMessage());
		}
    }

    @Transactional
    @Override
    public void callStaff(String data) {
        String id = ContextHolder.currentRestaurant().getId();
        List<String> tags =Lists.newArrayList();
        JSONObject jsonObject = JSONObject.parseObject(data);
        String restaurantTableId= jsonObject.getString("restaurantTableId");
        Integer type=jsonObject.getInteger("type");
        if(type==null){type=0;}
        RestaurantTable restaurantTable = restaurantTableService.get(restaurantTableId);
        if(restaurantTable!=null){
            tags.add(RestaurantSupport.createJpushTag(id,RestaurantStaff.ROLE_TYPE.FWY.getVal()));
            tags.add(RestaurantSupport.createJpushTag(id,RestaurantStaff.ROLE_TYPE.SYY.getVal()));
            pushMessageService.pushBusinessMessage(type,restaurantTable.getChineseName(),"");
        }
    }
}
